package jrunner;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Observable;
import java.util.Observer;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

public class PersonGUI extends Observable{
	
	//Swing elements
	private JDialog dialog;
	private JTextField nameTF;
	private JTextField ageTF;
	private JTextField heightTF;
	private JTextField weightTF;
	
	//Getters and Setters

	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public int getHeight() {
		return height;
	}
	public void setHeight(int height) {
		this.height = height;
	}
	public double getWeight() {
		return weight;
	}
	public void setWeight(double weight) {
		this.weight = weight;
	}
	public boolean isFemale() {
		return female;
	}
	public void setFemale(boolean female) {
		this.female = female;
	}
	public boolean isMale() {
		return male;
	}
	public void setMale(boolean male) {
		this.male = male;
	}
	
	//Important Variables
	private String name = "";
	private int age = 0;
	private int height = 0;
	private double weight = 0;
	private boolean female = false;
	private boolean male = false;
	
	private boolean alarmName = false;
	private boolean alarmAge = false;
	private boolean alarmHeight = false;
	private boolean alarmWeight = false;
	
	//Import Object
	private Observer person;
	
	public static void main (String[] args){
		
		PersonGUI window = new PersonGUI();
		window.buildWindow();
					
	}
	public void buildWindow(){
		
		//Adding the PersonObserver to the PersonGUI Observable
		person = Person.getPersonInstance(new ExportPersonDataToReadableFile());
		this.addObserver(person);
		
		dialog = new JDialog(); dialog.setTitle("Create New Person"); dialog.setSize(300, 300);
		JPanel panel = new JPanel();  panel.setLayout(new BorderLayout()); 

		JPanel north = new JPanel(); north.setBackground(Color.LIGHT_GRAY);
		JPanel name = new JPanel(); JLabel nameT = new JLabel("Name"); nameTF = new JTextField("                          ");
		name.add(nameT); name.add(nameTF); north.add(name); name.setBackground(Color.LIGHT_GRAY);
		
		//Create Center Panel
		JPanel center = new JPanel(); center.setLayout(new GridLayout(2,2));
		
		//Create Subsections of Center Panel
		JPanel age = new JPanel(); 
		JPanel ageSubPanel = new JPanel();
		
		JPanel height = new JPanel();
		JPanel heightSubPanel = new JPanel();
		
		JPanel weight = new JPanel();
		JPanel weightSubPanel = new JPanel();
		
		JPanel sex = new JPanel();
		JPanel sexSubPanel = new JPanel();
		
		//Define LayoutManagers for Subsections
		ageSubPanel.setLayout(new GridLayout(1,2));
		heightSubPanel.setLayout(new GridLayout(1,2));
		weightSubPanel.setLayout(new GridLayout(1,2));
		sexSubPanel.setLayout(new GridLayout(1,2)); 
		
		//Create and define RadioButtons from Subsection 'Sex'
		JRadioButton maleR = new JRadioButton("male"); 
		JRadioButton femaleR = new JRadioButton("female");
		maleR.addActionListener(new MaleRadioButton());
		femaleR.addActionListener(new FemaleRadioButton());
		
		ButtonGroup bGroup = new ButtonGroup();
		
		bGroup.add(maleR);
		bGroup.add(femaleR);
		
		//Create and define content of Subsections
		JLabel ageT = new JLabel("Age"); 
		JLabel heightT = new JLabel("Height");
		JLabel weightT = new JLabel("Weight");
		JLabel sexT = new JLabel("Sex");
		
		ageTF = new JTextField("        ");
		heightTF = new JTextField("        ");
		weightTF = new JTextField("        ");
		
		JLabel ageTT = new JLabel("years     ");
		JLabel heightTT = new JLabel("cm         "); 
		JLabel weightTT = new JLabel("kg           ");
		
		
		//Check the alarm flags.
		//If an alarm flag is true, color the text of
		//this flag red - if not, color it black.
		if(alarmName == true){
			nameT.setForeground(Color.RED);
		}else{
			nameT.setForeground(Color.BLACK);
		}
		if(alarmAge == true){
			ageT.setForeground(Color.RED);
		}else{
			ageT.setForeground(Color.BLACK);
		}
		if(alarmHeight == true){
			heightT.setForeground(Color.RED);
		}else{
			heightT.setForeground(Color.BLACK);
		}
		if(alarmWeight == true){
			weightT.setForeground(Color.RED);
		}else{
			weightT.setForeground(Color.BLACK);
		}
		
		//Add content to Subsections
		ageSubPanel.add(ageTF); ageSubPanel.add(ageTT);
		heightSubPanel.add(heightTF); heightSubPanel.add(heightTT);
		weightSubPanel.add(weightTF); weightSubPanel.add(weightTT);
		
		age.add(ageT); age.add(ageSubPanel);
		height.add(heightT); height.add(heightSubPanel);
		weight.add(weightT); weight.add(weightSubPanel);
		
		sexSubPanel.add(femaleR);sexSubPanel.add(maleR);
		sex.add(sexT); sex.add(sexSubPanel); 
		
		//Color Background of Subsections and Contents
		age.setBackground(Color.ORANGE); 
		ageSubPanel.setBackground(Color.ORANGE);
		height.setBackground(Color.WHITE);
		heightSubPanel.setBackground(Color.WHITE);
		weight.setBackground(Color.WHITE); 
		weightSubPanel.setBackground(Color.WHITE);
		sex.setBackground(Color.ORANGE);
		sexSubPanel.setBackground(Color.ORANGE);
		
		maleR.setBackground(Color.ORANGE);
		femaleR.setBackground(Color.ORANGE);
		
		//Add all Subsections to CENTER Panel
		center.add(age);
		center.add(height);
		center.add(weight);
		center.add(sex); 
		
		
		//Create Panel South and its' buttons.
		JPanel south = new JPanel(); south.setBackground(Color.BLACK);
		
		JButton ok = new JButton("OK"); JButton chancel = new JButton("Chancel");
		ok.addActionListener(new OKButton()); chancel.addActionListener(new ChancelButton());
		south.add(ok); south.add(chancel);
		
		//Add all Subsections to Panel
		panel.add(north,BorderLayout.NORTH);
		panel.add(center,BorderLayout.CENTER); 
		panel.add(south, BorderLayout.SOUTH);
				
		dialog.setContentPane(panel);
		dialog.setVisible(true);
		
	}
	
	class OKButton implements ActionListener{

		public void actionPerformed(ActionEvent e) {
			
			alarmName = false;
			alarmAge = false;
			alarmHeight = false;
			alarmWeight = false;
			
			//Parse all input information and save it in a variable.
			//If the input value is not correct a numberFormatException is thrown.
			//The catch block activates the alarmFlag of the concerned input variable.
			name = nameTF.getText().trim();
			
			if(name.isEmpty() || name == null){
				alarmName = true;
			}
			
			try{
				age = Integer.parseInt(ageTF.getText().trim());
			}catch(NumberFormatException numEx1){
				System.out.println("Entering NumberFormatException Age");
				alarmAge = true;
			}
			
			try {
				height = Integer.parseInt(heightTF.getText().trim());
			} catch (NumberFormatException numEx2) {
				System.out.println("Entering NumberFormatException Height");
				alarmHeight = true;
			}
			try {
				weight = Double.parseDouble(weightTF.getText().trim());
			} catch (NumberFormatException numEx3) {
				System.out.println("Entering NumberFormatException Weight");
				alarmWeight = true;			
				
			}
				
			dialog.dispose();
		
			//If an alarmFlag was activated. A new window is opened.
			if(alarmName == true || alarmAge == true || alarmHeight == true ||  alarmWeight == true){
				buildWindow();
			}
//			All delivered information is ok. The next step is the delivering
//			to the listener.
			else
			{
				
				setChanged(); //hier gibt es ein Problem
				notifyObservers(); //verdammt -> NullPointerException.
				System.out.println("Observers Notified.");
			}
		}
		
	}
	class ChancelButton implements ActionListener{

		public void actionPerformed(ActionEvent ev) {
			dialog.dispose();
			System.out.println("Erfolgreich geschlossen.");	
		}		
	}
	class MaleRadioButton implements ActionListener{

		public void actionPerformed(ActionEvent ae0) {
			female = false;
			male = true;
		}	
	}
	class FemaleRadioButton implements ActionListener{

		public void actionPerformed(ActionEvent ae1) {
			male = false;
			female = true;
		}	
	}
}

